package com.xiaozhen.tcp.zookeeper;

import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: zhenweisheng
 * @Date: 2020/7/31 13:55
 */
public class ZookeeperTester {

    public static void main(String[] args) throws UnknownHostException, InterruptedException {
        // 获取zookeeper注册地址
        String registryAddress = "localhost:2181";
        // 将本机netty服务实例注册到zookeeper
        ServiceRegistry serviceRegistry = new ServiceRegistry(registryAddress);

        // 获取本机ip
//    	String ip = InetAddress.getLocalHost().getHostAddress();
        String ip1 = "192.168.1.1";
        String ip2 = "192.168.1.2";
        String ip3 = "192.168.1.3";
        serviceRegistry.registerToZookeeper(ip1 + ":" + 9888);
        serviceRegistry.registerToZookeeper(ip2 + ":" + 9888);
       //serviceRegistry.registerToZookeeper(ip3 + ":" + 9888);


        ServiceDiscovery serviceDiscovery = new ServiceDiscovery(registryAddress);
        Map<String, Integer> routeMap = new HashMap<String, Integer>();
        for (int i = 0; i < 10; i++) {
            String address = serviceDiscovery.discover();
            if (!routeMap.containsKey(address)) {
                routeMap.put(address, 0);
            }
            routeMap.put(address, routeMap.get(address) + 1);
        }
        System.out.println("获取10次服务命中情况:");
        for (Map.Entry<String, Integer> entry : routeMap.entrySet()) {
            System.out.println(entry.getKey() + ":" + entry.getValue());
        }
    }


}

